package net.cyue.nest.nestjava.platform.vertx;

import io.vertx.core.http.HttpServerRequest;
import net.cyue.nest.nestjava.common.data.http.HTTPRequestBody;
import net.cyue.nest.nestjava.common.data.http.HTTPRequestParams;
import net.cyue.nest.nestjava.common.enums.RequestMethod;
import net.cyue.nest.nestjava.core.adapter.AbstractHTTPRequest;

import java.util.Map;

@Deprecated
public class VerticleHTTPRequest
  extends AbstractHTTPRequest<HttpServerRequest> {
  public VerticleHTTPRequest(HttpServerRequest request) {
    this.request = request;
  }

  @Override
  public String scheme() {
    return this.request.scheme();
  }

  @Override
  public RequestMethod method() {
    final var methodName = this.request.method().name().toUpperCase();
    return RequestMethod.valueOf(methodName);
  }

  @Override
  public String uri() {
    return this.request.uri();
  }

  @Override
  public String path() {
    return this.request.path();
  }

  @Override
  public HTTPRequestParams params() {
    final var params = this.request.params();
    final var paramEntries = params.entries();
    final var p = new HTTPRequestParams();
    for (final var e : paramEntries) {
      p.put(e.getKey(), e.getValue());
    }
    return p;
  }

  @Override
  public HTTPRequestBody body() {
    final var body = this.request.body();
    final var requestBody = new HTTPRequestBody();

    // TODO: 转同步
    body.onComplete((res) -> {
      final var result = res.result();
      final var iterator = result.toJsonObject().stream().iterator();
      Map.Entry<String, Object> v;
      while(iterator.hasNext()) {
        v = iterator.next();
        requestBody.put(v.getKey(), v.getValue());
      }
    });
    return requestBody;
  }

  @Override
  public String getHeader(String name) {
    return this.request.getHeader(name);
  }

  @Override
  public String getParameter(String name) {
    return this.request.getParam(name);
  }
}
